package com.hc.controller;

import com.hc.bean.Result;
import com.hc.bean.ResultUtil;
import com.hc.dto.UserLoginDTO;
import com.hc.enums.ResultEnum;
import com.hc.service.UserService;
import com.hc.utils.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 用户控制器
 * @author 梁云亮
 */
@Api(value = "用户控制器", tags = "用户 User 控制器")
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "getCheckCode：获取验证码", notes = "采用电话号码登录时，返回6位的验证码", httpMethod = "GET")
    @GetMapping("/v1/getCheckCode")
    public Result<String> getCheckCode(HttpSession session) {
        String checkCode = RandomUtil.genString(1, 6);
        session.setAttribute("checkCode", checkCode);
        return ResultUtil.success(200, checkCode);
    }

    @ApiOperation(value = "getVerifyCode：获取图片形式的验证码", notes = "获取图片形式的验证码", httpMethod = "GET")
    @RequestMapping("/v1/getVerifyCode")
    public void getVerifyCode(HttpSession session, HttpServletResponse response) throws IOException {
        ////生成验证码，6位
        //String verifyCode = VerifyCodeUtil.generateVerifyCode(4);
        ////验证码放入session
        //session.setAttribute("verifyCode", verifyCode);
        ////验证码存入图片
        //response.setContentType("image/png");
        //ServletOutputStream outputStream = response.getOutputStream();
        //VerifyCodeUtil.outputImage(100, 39, outputStream, verifyCode);
    }

    @ApiOperation(value = "getWechatLoginErcodeImg：获取微信登录二维码", notes = "获取微信登录二维码", httpMethod = "GET")
    @RequestMapping("/v1/getWechatLoginErcodeImg")
    public void getWechatLoginErcodeImg() {

    }

    @ApiOperation(value = "login：用户登录", notes = "处理用户登录请求：成功打开后台首页，失败打开登录页面", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "username", value = "用户名", required = true, dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(paramType = "query", name = "password", value = "用户名", required = true, dataType = "String", dataTypeClass = String.class)
    })
    @PostMapping("/login")
    public Result<UserLoginDTO> login(HttpSession session, @RequestParam("t") String type,
                                      @RequestParam(value = "tel", required = false) String tel,
                                      @RequestParam(value = "checkCode", required = false) String checkCode,
                                      @RequestParam(value = "username", required = false) String username,
                                      @RequestParam(value = "password", required = false) String password) {
        switch (type) {
            case "phoneLogin":
                String checkCode0 = session.getAttribute("checkCode").toString();
                if (!checkCode0.equals(checkCode)) {
                    return ResultUtil.fail(400,"校验码错误");
                }
                break;
            case "accountLogin":
                break;
            case "wechatLogin":
                break;
            default:
        }
        //UserLoginDTO user = userService.login(null);
        //return ResultUtil.success(200, "登录成功", user);
        return null;
    }
    //////////////////////////////////////////////////////////////////////


    @ApiOperation(value = "checkVerifyCode：校验验证码", notes = "校验验证码", httpMethod = "GET")
    @GetMapping("/checkVerifyCode")
    public Result checkVerifyCode(String code, HttpSession session) {
        String tmp = (String) session.getAttribute("verifyCode");
        if (tmp == null || tmp.length() == 0) {
            return ResultUtil.fail(ResultEnum.INTERNAL_SERVER_ERROR);
        }
        if (tmp.equalsIgnoreCase(code)) {
            return ResultUtil.success(ResultEnum.CHECK_CODE_SUCCESS);
        } else {
            return ResultUtil.fail(ResultEnum.CHECK_CODE_ERROR);
        }
    }

    @ApiOperation(value = "isUsernameExists：判断指定名称的用户是否存在", notes = "判断指定名称的用户是否存在", httpMethod = "GET")
    @GetMapping("/isUsernameExists")
    public Result<Boolean> isUsernameExists(String username) {
        //boolean exists = userService.getCountByUsername(username) >= 1 ? true : false;
        //return ResultUtil.success(200, null, exists);
        return null;
    }

    ///**
    // * 查询指定id的用户的所有信息
    // *
    // * @param id 用户id
    // * @return 用户信息
    // */
    //@GetMapping("/getUserById")
    //public User getUserById(Integer id) {
    //    User user = userService.getById(id);
    //    return user;
    //}
    //@PostMapping("/save")
    //public User save(User user) {
    //    System.out.println("save");
    //    System.out.println(user);
    //    return new User();
    //}
    //
    //
    ///**
    // * 打开后台管理员登录页面
    // *
    // * @return
    // */
    //@GetMapping
    //@ApiOperation(value = "showLogin：打开用户登录页面", notes = "打开用户登录页面", httpMethod = "GET")
    //public String toLogin() {
    //    return "/user/login";
    //}
    //
    //
    //@GetMapping("/logout")
    //@ApiOperation(value = "logout:退出登录", notes = "处理退出登录请求：成功打开后台首页", httpMethod = "POST")
    //public String logout(HttpSession session) {
    //    session.setAttribute("user", null);
    //    return "/user/index";
    //}

}
